var data1 = []
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;

layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem = []
        for (let item of jsons) {
            let tmparr = {}
            tmparr["value"] = item["code"];
            tmparr["name"] = item["name"];
            if (item["users"]) {
                for (let it of item["users"]) {
                    item["list"].push(it)
                }
            }
            if (item["list"] && item["list"].length) {
                tmparr["children"] = getAllJson(item["list"]);
                tmpitem.push(tmparr)
            } else {
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }


    let salesmanList = [];
    let statusList = [];
    let deptSalesmanList=[];
    (function () {
        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();
        statusList = getxmselect("#status", "boxStatus", [
            {name: '草稿箱', value: 1},
            {name: '待批箱', value: 2},
            {name: '已批箱', value: 3},
            {name: '完成箱', value: 4}
        ]);
        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            salesmanList = getxmselect("#salesmanCode", "salesmanCode", userTree);
        } else {
            salesmanList = getxmselect("#salesmanCode", "salesmanCode", [{name: user.name, value: user.code}]);
        }

        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", deptTree);
        } else {
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", [{name: user.name, value: user.code}]);
        }

    })()

    form.render();//表单刷新
    var data2 = []
    table.render({
        elem: '#foreignexchangesettlementselect'
        , data: data2
        , url: 'billChange/selectCostByPage'
        , toolbar: '#foreignexchangesettlementtoolbar'
        , totalRow: true
        , cols: [[
            {field: 'invoice_no', title: '发票号码', sort: true, width: 180, totalRowText: '合计'}
            , {field: 'contract_no', title: '销售合同号', sort: true, width: 240}
            , {field: 'price_term', title: '价格术语', sort: true, width: 120}
            , {field: 'money_kind', title: '币种', sort: true, width: 120}
            , {field: 'usd_rate', title: '美元汇率', sort: true, width: 120}
            , {field: 'rmb_rate', title: '人民币汇率', sort: true, width: 120}
            , {field: 'cost_money', title: '金额小计', sort: true, width: 120, totalRow: true}
            , {field: 'usb', title: '美元小计', sort: true, width: 120, totalRow: true}
            , {field: 'rmb', title: '人民币小计', sort: true, width: 120, totalRow: true}
            , {field: 'customer_name', title: '客户名', sort: true, width: 200}
            , {field: 'invoice_date', title: '发票日期', sort: true, width: 120}
            , {field: 'salesman', title: '业务员', sort: true, width: 120}
            , {field: 'cost_desc', title: '费用描述', width: 250, sort: true}
        ]]
        , id: 'foreignexchangesettlementtableid'
        , height: $(document).height() - $('#foreignexchangesettlementselect').offset().top - 50
        , limits: [100, 300, 500]
        , limit: 100
        , page: true
    });
//发票导出excel
    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url: "billChange/getCostContractStatisticsOneInfo",
            success: function (res) {
                if (res.status == 200) {
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '结汇发票费用统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });
    form.on('submit(submitform)', function (data) {
        table.reload('foreignexchangesettlementtableid', {
            data: data1,
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: data.field
        }, 'data');
        return false;
    });

    $(document).ready(function () {
        $.ajax({
            url: 'billChange/selectAllUser',
            type: 'GET',
            success: function (date) {
                var sm = date.data
                for (let i in sm) {
                    $("#salesman_code").append("<option value='" + sm[i].code + "'>" + sm[i].name + "</option>")
                }
                form.render();
            }
        });

    });

    document.getElementById('clear').onclick = function () {
        statusList.setValue([]);
        salesmanList.setValue([]);
        deptSalesmanList.setValue([]);
    };
})